<?php

/*
Zoo Management Simulation
The cageProcess file manages requests sent from the Cage UI by securing doors, toggling the lighting
*/

include("dbconnect.php");
if ($_REQUEST['submit'] === "Return to Zoo page"){
	header('Location: index.php');
}

else if ($_REQUEST['submit'] == "Secure doors"){
	$securesql = "UPDATE `entry` SET `isEntryOpen`=0 WHERE `enclosureId`=".$_REQUEST['cageid'];
	$dbh->exec($securesql);
	header('Location: cage.php?cageid='.$_REQUEST['cageid'].'&cagenumber='.$_REQUEST['cagenumber']);
}

else if ($_REQUEST['submit'] == "Toggle lighting"){
	if ($_REQUEST['lighting'] == 1){
		$lightsql = "UPDATE `enclosure` SET `isEnclosureLightsOn`=0 WHERE `enclosureId`=".$_REQUEST['cageid'];
		$dbh->exec($lightsql);
		header('Location: cage.php?cageid='.$_REQUEST['cageid'].'&cagenumber='.$_REQUEST['cagenumber']);
	}
	elseif($_REQUEST['lighting'] == 0){
		$lightsql = "UPDATE `enclosure` SET `isEnclosureLightsOn`=1 WHERE `enclosureId`=".$_REQUEST['cageid'];
		$dbh->exec($lightsql);
		header('Location: cage.php?cageid='.$_REQUEST['cageid'].'&cagenumber='.$_REQUEST['cagenumber']);

	}
}

else if ($_REQUEST['submit'] == "Toggle door"){
	if ($_REQUEST['entrystatus'] == 1){
		$entrysql = "UPDATE `entry` SET `isEntryOpen`=0 WHERE `entryId`=".$_REQUEST['entryid'];
		$dbh->exec($entrysql);
		header('Location: cage.php?cageid='.$_REQUEST['cageid'].'&cagenumber='.$_REQUEST['cagenumber']);
	}
	else if ($_REQUEST['entrystatus'] == 0){
		$entrysql = "UPDATE `entry` SET `isEntryOpen`=1 WHERE `entryId`=".$_REQUEST['entryid'];
		$dbh->exec($entrysql);
		header('Location: cage.php?cageid='.$_REQUEST['cageid'].'&cagenumber='.$_REQUEST['cagenumber']);
	}
}

else if ($_REQUEST['submit'] == "Toggle Human"){
	if ($_REQUEST['humandetector'] == 1){
		$humansql = "UPDATE `enclosure` SET `enclosureHasPerson`=0 WHERE `enclosureId`=".$_REQUEST['cageid'];
		$dbh->exec($humansql);
		header('Location: cage.php?cageid='.$_REQUEST['cageid'].'&cagenumber='.$_REQUEST['cagenumber']);
	}
	elseif($_REQUEST['humandetector'] == 0){
		$humansql = "UPDATE `enclosure` SET `enclosureHasPerson`=1 WHERE `enclosureId`=".$_REQUEST['cageid'];
		$dbh->exec($humansql);
		header('Location: cage.php?cageid='.$_REQUEST['cageid'].'&cagenumber='.$_REQUEST['cagenumber']);

	}
}

else if ($_REQUEST['submit'] == "Toggle Animal"){
	if ($_REQUEST['animaldetector'] == 1){
		$animalsql = "UPDATE `enclosure` SET `enclosureHasAnimal`=0 WHERE `enclosureId`=".$_REQUEST['cageid'];
		$dbh->exec($animalsql);
		header('Location: cage.php?cageid='.$_REQUEST['cageid'].'&cagenumber='.$_REQUEST['cagenumber']);
	}
	elseif($_REQUEST['animaldetector'] == 0){
		$animalsql = "UPDATE `enclosure` SET `enclosureHasAnimal`=1 WHERE `enclosureId`=".$_REQUEST['cageid'];
		$dbh->exec($animalsql);
		header('Location: cage.php?cageid='.$_REQUEST['cageid'].'&cagenumber='.$_REQUEST['cagenumber']);

	}
}

$dbh = null;?>